package com.szbmyoubao.app.ui.service_setting

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.szbmyoubao.app.storage.AppStorage
import com.szbmyoubao.app.utils.AppTools
import com.ellecity06.common.base.BaseMvvmActivity
import com.ellecity06.common.base.BaseViewModel
import com.ellecity06.common.widgets.dialog.showAlert
import com.ellecity06.cwhiskymall.databinding.ActivityServiceSettingBinding

/**
 * @author ellecity06
 * @time 2024/11/14 10:32
 * @des
 */
class ServiceSettingActivity : BaseMvvmActivity<ActivityServiceSettingBinding, BaseViewModel>() {
    private val adapter: ServiceSettingAdapter by lazy { ServiceSettingAdapter() }
    private val serviceList: MutableList<ServiceModel> by lazy {
        mutableListOf(
            ServiceModel(
                name = "测试环境",
                apiUrl = "http://192.168.1.33:10008/",
                webUrl = "",

                key = "test-online"
            ),
            ServiceModel(
                name = "生产线上",
                apiUrl = "https://digital.d1gjrkj.com/",
                webUrl = "",
                key = "online-release"
            )
        )
    }

    override fun inflateBinding(): ActivityServiceSettingBinding {
        return ActivityServiceSettingBinding.inflate(layoutInflater)
    }

    override fun initialize(savedInstanceState: Bundle?) {
        title = "环境切换"
        binding.rv.layoutManager = LinearLayoutManager(this)
        binding.rv.adapter = adapter
        adapter.submitList(serviceList)
        adapter.setOnItemClickListener { _, _, position ->
            val item = adapter.getItem(position)
            showAlert(content = "确定要切换到[${item?.name}] 吗？") {
                AppStorage.Companion.serviceKey = item?.key
                AppStorage.Companion.serviceConfig = item
                AppTools.Companion.restartApplication()
            }
        }
    }
}